import { defineStore } from "pinia";
import { addRess, getRess, updateRess, removeRess } from "@/api/ress";
import type { AddressEditInfo } from 'vant'
import { showDialog } from 'vant'


type state = {
    ressData: ress[],
    flag: boolean
}
export const useRess = defineStore(
    'ress',
    {
        state: (): state => {
            return {
                // 地址数据
                ressData: [],
                // 如果确认订单地址跳转过来的
                flag: false
            }
        },
        getters: {},
        actions: {
            changeFlagF() {
                this.flag = false
            },
            changeFlagT() {
                this.flag = true
            },
            async fetchAddRess(arr: AddressEditInfo) {
                let res = await addRess(localStorage.getItem('userId') as string, arr.name, arr.tel, arr.province, arr.city, arr.county, arr.addressDetail, arr.isDefault as boolean);
                console.log(res);

            },
            async fetchGetRess() {
                let res = await getRess(localStorage.getItem("userId") as string);
                this.ressData = res.data;
            },
            async fetchUpdateRess(id: string, arr: AddressEditInfo) {
                // res是源数据
                let res = this.ressData.find(item => item.addressid == id);
                if (res) {
                    // 新数据修改源数据
                    res.name = arr.name;
                    res.tel = arr.tel;
                    res.city = arr.city;
                    res.province = arr.province;
                    res.county = arr.country;
                    res.isDefault = arr.isDefault as boolean;
                    res.addressDetail = arr.addressDetail;
                    // 如果是默认地址
                    if (res.isDefault) {
                        // 把其他本地地址的默认取消
                        this.ressData.forEach(item => {
                            if (item.addressid != id) {
                                item.isDefault = false;
                            }
                        });
                        // 根据最新的本地地址数据发请求
                        for (let i = 0; i < this.ressData.length; i++) {
                            await updateRess(this.ressData[i].addressid, this.ressData[i].userid, this.ressData[i].name, this.ressData[i].tel, this.ressData[i].province, this.ressData[i].city, this.ressData[i].county, this.ressData[i].addressDetail, this.ressData[i].isDefault)
                        }
                        // 如果取消默认地址
                    } else {
                        await updateRess(res.addressid, res.userid, res.name, res.tel, res.province, res.city, res.county, res.addressDetail, res.isDefault)
                    }
                }

            },
            changeDe(id: string) {
                let data = this.ressData.find(item => item.addressid == id)
                if (data) {
                    data.isDefault = !data.isDefault
                }
            },
            async removes(id: string) {
                let res = await removeRess(id)
            },

            // 先改变服务端数据 但是不改变本地数据  确认订单页面
            async queren(id: string) {
                let res = this.ressData.find(item => item.addressid == id);
                let isDe = this.ressData.find(item => item.isDefault);
                // 如果存在默认地址
                if (isDe) {
                    // 改变服务端的默认地址
                    await updateRess(isDe.addressid, isDe.userid, isDe.name, isDe.tel, isDe.province, isDe.city, isDe.county, isDe.addressDetail, false);
                }
                await updateRess(res!.addressid, res!.userid, res!.name, res!.tel, res!.province, res!.city, res!.county, res!.addressDetail, true);
            },
            // 因为服务端被改变 本地数据没有发生改变 根据本地数据修改服务端
            async huifu() {
                // 根据最新的本地地址数据发请求
                for (let i = 0; i < this.ressData.length; i++) {
                    await updateRess(this.ressData[i].addressid, this.ressData[i].userid, this.ressData[i].name, this.ressData[i].tel, this.ressData[i].province, this.ressData[i].city, this.ressData[i].county, this.ressData[i].addressDetail, this.ressData[i].isDefault)
                }
            }
        }
    }
)